package com.zjy.pgcms.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class FileUtil {
    private static String separator = System.getProperty("file.separator");

    public static String saveAvatar(MultipartFile file,String baseUrl, Long id) {
        try {
            String fileName = id+"_"+file.getOriginalFilename();
            String filePath = baseUrl + separator + fileName;
            File avatarFile = new File(filePath);
            if (!avatarFile.getParentFile().exists()){
                avatarFile.getParentFile().mkdirs();
            }
            file.transferTo(avatarFile);
            String hostAddress = "127.0.0.1";
            return "http://"+hostAddress + "/file/" +fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
